/**
 * org.assess.oa.system.action.TbMenuAction
 */
package org.assess.oa.system.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.assess.core.action.BaseAction;
import org.assess.core.service.ServiceResult;
import org.assess.oa.system.bean.TbMenu;
import org.assess.oa.system.bean.TbMenuItem;
import org.assess.oa.system.bean.TbSystem;
import org.assess.oa.system.service.TbMenuService;
import org.assess.oa.system.service.TbSystemService;

/**
 * @description 菜单管理的Action实现类
 * @author XiaLongxiang
 * @createtime 2012-6-6 下午11:19:50
 */
public class TbMenuAction extends BaseAction {
    /** 序列化号 */
    private static final long serialVersionUID = -1938750103552293758L;
    
    /** 节点的层级（根节点值为0） */
    private String n_level;
    
    /** 角色ID */
    private String role_id;
    
    /** TbMenu的JavaBean对象 */
    private TbMenu menu;
    
    /** TbMenuItem的JavaBean对象 */
    private TbMenuItem menuitem;
    
    /** 系统信息列表 */
    private List<TbSystem> systemList;
    
    /** 菜单管理的Service层对象 */
    private TbMenuService tbMenuService;
    
    /** 系统管理的Service层对象 */
    private TbSystemService tbSystemService;
    
    /**
     * 跳转到菜单管理页面
     * @return
     */
    public String showMenuJsp() {
        TbSystem system = new TbSystem();
        system.setValid_flag("1");
        this.systemList = this.tbSystemService.queryTbSystemAsList(system);
        return SUCCESS;
    }
    
    /**
     * 加载菜单列表
     * @return
     */
    public String listMenu() {
        int depth = StringUtils.isBlank(n_level) ? 0 : Integer.parseInt(n_level) + 1;
        String jsonString = this.tbMenuService.queryTbMenuAsList(depth, menu);
        super.setJsonString(jsonString);
        return SUCCESS;
    }
    
    /**
     * 加载单个菜单信息，用于编辑修改页面
     * @return
     */
    public String loadMenu() {
        if (menu != null && StringUtils.isNotBlank(menu.getMenu_id())) {
            super.setEdit(true);
            menu = this.tbMenuService.queryTbMenuById(menu);
        }
        else {
            super.setEdit(false);
        }
        return SUCCESS;
    }
    
    /**
     * 加载单个菜单项信息，用于编辑修改页面
     * @return
     */
    public String loadMenuItem() {
        if (menuitem != null && StringUtils.isNotBlank(menuitem.getMenuitem_id())) {
            super.setEdit(true);
            menuitem = this.tbMenuService.queryTbMenuItemById(menuitem);
        }
        else {
            super.setEdit(false);
        }
        return SUCCESS;
    }
    
    /**
     * 保存菜单信息
     * @return
     */
    public String saveMenu() {
        try {
            ServiceResult result = this.tbMenuService.saveTbMenu(menu, super.isEdit());
            if (result == ServiceResult.save_success) {
                super.setMessage(super.getText("G_SAVE_001", new String[] {super.getText("ST_MENU_000")}));
            }
            else {
                super.setMessage(super.getText("G_SAVE_002", new String[] {super.getText("ST_MENU_000")}));
            }
        }
        catch (Exception ex) {
            super.setMessage(super.getText("G_SAVE_002", new String[] {super.getText("ST_MENU_000")}));
        }
        
        return SUCCESS;
    }
    
    /**
     * 保存菜单项信息
     * @return
     */
    public String saveMenuItem() {
        try {
            ServiceResult result = this.tbMenuService.saveTbMenuItem(menuitem, super.isEdit());
            if (result == ServiceResult.save_success) {
                super.setMessage(super.getText("G_SAVE_001", new String[] {super.getText("ST_MENUITEM_000")}));
            }
            else {
                super.setMessage(super.getText("G_SAVE_002", new String[] {super.getText("ST_MENUITEM_000")}));
            }
        }
        catch (Exception ex) {
            super.setMessage(super.getText("G_SAVE_002", new String[] {super.getText("ST_MENUITEM_000")}));
        }
        
        return SUCCESS;
    }
    
    /**
     * 检查菜单信息：检查菜单编码是否已存在
     * @return
     */
    public String checkMenu() {
        String jsonString = this.tbMenuService.checkTbMenu(menu);
        super.setJsonString(jsonString);
        return SUCCESS;
    }
    
    /**
     * 检查菜单项信息：检查菜单项编码是否已存在
     * @return
     */
    public String checkMenuItem() {
        String jsonString = this.tbMenuService.checkTbMenuItem(menuitem);
        super.setJsonString(jsonString);
        return SUCCESS;
    }
    
    /**
     * 删除菜单信息
     * @return
     */
    public String deleteMenu() {
        if (menu == null) {
            menu = new TbMenu();
        }
        
        menu.setMenu_id(super.getId());
        
        try {
            String jsonString = this.tbMenuService.deleteTbMenu(menu);
            super.setJsonString(jsonString);
        }
        catch (Exception ex) {
            
        }
        
        return SUCCESS;
    }
    
    /**
     * 删除菜单项信息
     * @return
     */
    public String deleteMenuItem() {
        if (menuitem == null) {
            menuitem = new TbMenuItem();
        }
        
        menuitem.setMenuitem_id(super.getId());
        
        try {
            String jsonString = this.tbMenuService.deleteTbMenuItem(menuitem);
            super.setJsonString(jsonString);
        }
        catch (Exception ex) {
            
        }
        
        return SUCCESS;
    }
    
    /**
     * 加载菜单列表，并返回其JSON字符串，用于ztree的下拉树
     * @return
     */
    public String listMenuAsJsonArray() {
        String jsonString = this.tbMenuService.queryTbMenuAsJsonArray(menu);
        super.setJsonString(jsonString);
        return SUCCESS;
    }
    
    /**
     * 根据系统编码，加载该系统所包含的所有菜单和菜单项
     * @return
     */
    public String listMenuAsRoleAll() {
        String jsonString = this.tbMenuService.queryMenuAsRoleAll(role_id, menu);
        super.setJsonString(jsonString);
        return SUCCESS;
    }
    
    public void setN_level(String n_level) {
        this.n_level = n_level;
    }
    
    public String getRole_id() {
        return role_id;
    }
    
    public void setRole_id(String role_id) {
        this.role_id = role_id;
    }
    
    public TbMenu getMenu() {
        return menu;
    }
    
    public void setMenu(TbMenu menu) {
        this.menu = menu;
    }
    
    public TbMenuItem getMenuitem() {
        return menuitem;
    }
    
    public void setMenuitem(TbMenuItem menuitem) {
        this.menuitem = menuitem;
    }
    
    public List<TbSystem> getSystemList() {
        return systemList;
    }
    
    public void setTbMenuService(TbMenuService tbMenuService) {
        this.tbMenuService = tbMenuService;
    }
    
    public void setTbSystemService(TbSystemService tbSystemService) {
        this.tbSystemService = tbSystemService;
    }
}
